package xyz.liut.liutlibraries.baseclass;

import android.content.Context;
import android.view.View;
import android.widget.BaseAdapter;
import android.widget.ListView;

import java.util.List;

/**
 * Created by liut_ on 2016/5/19.
 */
public abstract class MyBaseAdapter<T> extends BaseAdapter {

    public Context context;
    public List<T> dataBeans;
    public View childView;

    public MyBaseAdapter(Context context, List<T> dataBeans) {
        this.context = context;
        this.dataBeans = dataBeans;
    }

    @Override
    public int getCount() {
        return dataBeans.size();
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public Object getItem(int position) {
        return dataBeans.get(position);
    }


    /**
     * 用于更新要更新的item
     *
     * @param itemIndex 要item的下标
     */
    public void updateView(int itemIndex, ListView listView, List<T> dataBeans) {
        this.dataBeans = dataBeans;
        // 得到第1个可见控件的位置,记住是第1个可见控件。而不是第1个控件
        int visiblePosition = listView.getFirstVisiblePosition();
        // 得到你需要更新item的View
        childView = listView.getChildAt(itemIndex - visiblePosition);
    }
}
